#   setwd("C:/Users/Jef/Desktop/Applied Statistics")
setwd("D:/Mijn Documenten/applied statistics - project")
library(qcc)

#PROCESS CAPABILITY ANALYSIS:

paper <- read.table("paperfranklindata1.txt")

#informal check for outliers, Box and Whisker Plot
boxplot(paper, horizontal = TRUE, main="Box-and-Whisker plot of initial data")

#Kernel density plot
plot(density(paper$V1),main="Kernel density estimate of initial data",col="red",lwd=3)

#Normal probability plot
qqnorm(paper$V1,main="Normal probability plot of initial data", pch=19,cex=1,fg="red")
qqline(paper$V1,lwd=3,col="blue",lty="dashed")

#Confidence levels
samplenumber <- rep(1:35, each=1)
paperdata <- qcc.groups(paper$V1, samplenumber)
paperqcc <- qcc(paperdata,main="Process capability analysis of initial data", type="xbar",spec.limits=c(4.920, 4.980)) 
process.capability(paperqcc, spec.limits=c(4.920, 4.980))


#CONTROL CHARTS
paper2 <- read.table("paperfranklindataallekollommen.txt")

#get center of first 15 samples
stats.R(paper2[1:15,2:6])
stats.xbar(paper2[1:15,2:6])

#get standard deviations of firs 15 samples
sd.xbar(paper2[1:15,2:6])
sd.R(paper2[1:15,2:6])

#calculating standard control limits x-bar chart
LCLXs <-4.958133-(3*0.01162417)/(sqrt(5))
UCLXs <- 4.958133+(3*0.01162417)/(sqrt(5))
LCLRs <-0.367*0.02566667/2.326
UCLRs <- 5.484*0.02566667/2.326

#calculating the control limits they used
LCLXp <-4.958133-0.577*0.02566667
UCLXp <-4.958133+0.577*0.02566667
LCLRp <- 0.000*0.02566667
UCLRp <- 2.115* 0.02566667

#getting the control charts with the limits used by the paper 
paperqccX1 <-qcc(paper2[1:15,2:6], type="xbar", center=4.958, limits=c(4.9433,4.9729))
paperqccR1 <-qcc(paper2[1:15,2:6], type="R", center=0.02567, limits=c(0.0,0.0543))

paperqccX2 <-qcc(paper2[16:32,2:6], type="xbar", center=4.958, limits=c(4.9433,4.9729))
paperqccR2 <-qcc(paper2[16:32,2:6], type="R", center=0.02567, limits=c(0.0,0.0543))

paperqccX3 <-qcc(paper2[33:47,2:6], type="xbar", center=4.958, limits=c(4.9433,4.9729))
paperqccR3 <-qcc(paper2[33:47,2:6], type="R", center=0.02567, limits=c(0.0,0.0543))


#CAPABILITY STUDY LAST 15 SAMPLES
process.capability(paperqccX3, spec.limits=c(4.920, 4.980))